<?php

/**
 * This is the model class for table "{{fans}}".
 *
 * The followings are the available columns in table '{{fans}}':
 * @property integer $fans_id
 * @property integer $user_id
 * @property string $user_name
 * @property integer $fans_user_id
 * @property string $fans_user_name
 * @property integer $fans_time
 *
 * The followings are the available model relations:
 */
class Fans extends CActiveRecord
{
	public $isUpper; //标志是否为上游

	/**
	 * Returns the static model of the specified AR class.
	 * @return Fans the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{fans}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, user_name, fans_user_id, fans_user_name, fans_time', 'required'),
			array('user_id, fans_user_id, fans_time', 'numerical', 'integerOnly'=>true),
			array('user_name, fans_user_name', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('fans_id, user_id, user_name, fans_user_id, fans_user_name, fans_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user'=>array(self::BELONGS_TO,'User','fans_user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'fans_id' => 'Fans',
			'user_id' => 'User',
			'user_name' => 'User Name',
			'fans_user_id' => 'Fans User',
			'fans_user_name' => 'Fans User Name',
			'fans_time' => 'Fans Time',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('fans_id',$this->fans_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('user_name',$this->user_name,true);
		$criteria->compare('fans_user_id',$this->fans_user_id);
		$criteria->compare('fans_user_name',$this->fans_user_name,true);
		$criteria->compare('fans_time',$this->fans_time);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	public static function getUserByFansUid($userId,$pagination=10,$with='user'){
		//此方法效率比较低下，因为要多链接查询10次，但为了使用YII，所以暂时用此种解决办法，待日后改进。
		$criteria = new CDbCriteria(array(
			'condition'=>'t.user_id='.$userId . " and is_outdate = 'no'",
			'order'=>'fans_time DESC',
		));
		if($with){
			$criteria->with = $with;
		}
		$dataProviderFans=new CActiveDataProvider('Fans',array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=>$pagination,
			),
		));
		foreach($dataProviderFans->getData() as $fansHandle){
			$u = Upper::model()->find('user_id = ' . $fansHandle->user_id . ' and upper_user_id = ' . $fansHandle->fans_user_id);
			if($u !== null)
				$fansHandle->isUpper = true;
			else 
				$fansHandle->isUpper = false;
		}
		return $dataProviderFans;
	}

	public static function addFans($fansUserId,$userId,$isOutDate=true){
		$fansAR = self::loadModelByFansUserId($userId,$fansUserId);
		if($fansAR){//1、已经存在
			$fansAR->fans_time = time();
			if($isOutDate){ //1.1目的是要更新为过时
				if($fansAR->is_outdate == 'yes')return true;//1.1.1已过时，则返回
				else{//1.1.2未过时，则更新为过时的
					$fansAR->is_outdate = 'yes';
					if($fansAR->save())return true;
				}
			}else{//1.2目的是更新为未过时的
				if($fansAR->is_outdate == 'no')return true;//1.2.1未过时，则返回
				else{//1.2.2已过时，则更新为未过时的
					$fansAR->is_outdate = 'no';
					if($fansAR->save())return true;
				}
			}
		}else{//2、不存在则插入
			//unset($fansAR);	
			$fansAR = new Fans;
			$fansAR->is_outdate = $isOutDate ? 'yes' : 'no';
			$fansAR->user_id = $userId;
			$fansAR->fans_user_id = $fansUserId;
			$fansAR->fans_time = time();
			if($userId == Yii::app()->user->id){
				$fansAR->user_name = Yii::app()->user->name;
				$userName = User::model()->findByPk($fansUserId);
				$fansAR->fans_user_name = $userName->user_name;
			}else{
				$fansAR->fans_user_name = Yii::app()->user->name;
				$userName = User::model()->findByPk($userId);
				$fansAR->user_name = $userName->user_name;
			}
			if($fansAR->save(false));return true;
		}
		return false;
	}
	public static function loadModelByFansUserId($userId,$fansUserId){
		$fans = new Fans;
		return $fans->find('user_id = ' .$userId . ' and fans_user_id = '.$fansUserId);
	}
}